// api/modules/device.js
import {
	request
} from '@/utils/request'

export default {
	//上传智能体头像
	// api/modules/device.js
	uploadIcon(file) {
		const userId = uni.getStorageSync('userId');
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: 'http://113.45.222.55:8081/api/device/upload/icon',
				filePath: file,
				name: 'file',
				header: {
					'User-Id': userId
				},
				success: (res) => {
					try {
						const data = JSON.parse(res.data);
						if (data.code === 200) {
							resolve(data);
						} else {
							reject(new Error(data.message || '上传失败'));
						}
					} catch (e) {
						reject(new Error('解析响应失败'));
					}
				},
				fail: (err) => {
					reject(new Error(err.errMsg || '上传失败'));
				}
			});
		});
	},

	// 创建智能体
	createDevice(data) {
		const userId = uni.getStorageSync('userId');
		return request({
			url: 'http://113.45.222.55:8081/api/device',
			method: 'POST',
			data,
			header: {
				'Content-Type': 'application/json',
				'User-Id': userId
			}
		});
	},

	// 获取用户智能体列表
	getUserDevices() {
		console.log('调用 getUserDevices API') // 添加这行
		return request({
			url: 'http://113.45.222.55:8081/api/device/list',
			method: 'GET',
			success: (res) => {
				console.log('getUserDevices API 响应:', res) // 添加这行
				return res
			}
		})
	},

	// 获取公开智能体列表
	getPublicDevices(params = {
		current: 1,
		size: 10
	}) {
		return request({
			url: 'http://113.45.222.55:8081/api/device/public',
			method: 'GET',
			params
		})
	},

	// 收藏智能体
	collect(deviceId) {
		const userId = uni.getStorageSync('userId')
		return request({
			url: `http://113.45.222.55:8081/api/device/${deviceId}/collect`,
			method: 'POST',
			header: {
				'User-Id': userId
			}
		})
	},

	// 取消收藏
	uncollect(deviceId) {
		return request({
			url: 'http://113.45.222.55:8081/api/device/${deviceId}/collect',
			method: 'DELETE'
		})
	}
}